package com.belazy.basics.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @version: 1.0
 * @author:tangcp
 * @date:2021/9/25 14:45
 * @description: 跨域配置
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config = new CorsConfiguration ();
        config.setAllowCredentials (true);
        config.addAllowedHeader ("*");
        config.addAllowedOrigin ("*");
        config.addAllowedMethod (HttpMethod.DELETE);
        config.addAllowedMethod (HttpMethod.OPTIONS);
        config.addAllowedMethod (HttpMethod.POST);
        config.addAllowedMethod (HttpMethod.GET);
        config.addAllowedMethod (HttpMethod.PUT);
        config.addAllowedMethod (HttpMethod.HEAD);
        config.addAllowedMethod (HttpMethod.PATCH);
        config.addAllowedMethod (HttpMethod.TRACE);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (new PathPatternParser ());
        source.registerCorsConfiguration ("/**",config);
        return new CorsWebFilter (source);
    }
}
